package ck.android.englishschool.popup;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import ck.android.englishschool.AppStatus;
import ck.android.englishschool.R;
import ck.android.englishschool.thread.WordDatailThread;
import ck.android.englishschool.thread.BaseThread.ThreadCallBack;
import ck.android.englishschool.vo.WordDetailVO;

public class WordTestActivity extends BaseActivity implements ThreadCallBack{
	private WordDatailThread thread;
	private String word;
	private WordDetailVO wordDetailVO;
	
	private LinearLayout main_layout;

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_THREAD_COMPLETED:
				layoutSetting();
				break;
				
			case MSG_THREAD_ERROR:
				
				break;
				
			case MSG_NETWORK_ERROR:
				
				break;
				
			case MSG_UPDATE_COMPLETED:
				
				break;
				
			default:
				
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_word_test);
		
		myapp = (AppStatus) getApplicationContext();
		context = getApplicationContext();
		
		intent = getIntent();
		word = intent.getStringExtra("title");
		wordDetailVO = new WordDetailVO();
		wordDetailVO.setTitle(word);
		init();
	}
	
	@Override
	public void init() {
		context = this;
		myapp.showDialogNo("데이터 로딩중", context);
		main_layout = (LinearLayout)findViewById(R.id.main_layout);
		
		dataLoad();
	}

	@Override
	public void dataLoad() {
		thread = new WordDatailThread(context, this, wordDetailVO);
		thread.start();
	}

	@Override
	public void layoutSetting() {
		ArrayList<WordDetailVO> rvo = new ArrayList<WordDetailVO>();
		rvo = myapp.getArrWordDetail(); //객체의 주소까지 복사해버린다....
		rvo = myapp.shuffleWord(rvo);
		int cnt = 0;
		int idCnt = 0;
		System.out.println(cnt = myapp.getArrWordDetail().size());
		for(int i=0; i<cnt; i++){
			final RelativeLayout layout = new RelativeLayout(WordTestActivity.this);
			RelativeLayout.LayoutParams layParam = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			layout.setLayoutParams(layParam);
			
			//선언
			TextView seq = new TextView(WordTestActivity.this);
			TextView text = new TextView(WordTestActivity.this);
			EditText edit = new EditText(WordTestActivity.this);
			
			//아이디값 넣기
			seq.setId(++idCnt);
			text.setId(++idCnt);
			edit.setId(++idCnt);

			//번호
			RelativeLayout.LayoutParams seqParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
			seqParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			seqParam.addRule(RelativeLayout.CENTER_VERTICAL);
			seqParam.setMargins(20, 0, 0, 0);
			seq.setLayoutParams(seqParam);
			seq.setText(i+".");
			
			//뜻
			RelativeLayout.LayoutParams textParam = new RelativeLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT );
			textParam.addRule(RelativeLayout.RIGHT_OF, seq.getId() );
			textParam.addRule(RelativeLayout.CENTER_VERTICAL);
			textParam.setMargins(20, 0, 0, 0);
			text.setLayoutParams(textParam);
			
			text.setText(rvo.get(i).getkWord()+":"+myapp.getArrWordDetail().get(i).getkWord());
			
			//영어
			RelativeLayout.LayoutParams editParam = new RelativeLayout.LayoutParams(500, LayoutParams.WRAP_CONTENT );
			editParam.addRule(RelativeLayout.RIGHT_OF, text.getId());
			editParam.addRule(RelativeLayout.CENTER_VERTICAL);
			editParam.setMargins(0, 0, 0, 0);
			edit.setLayoutParams(editParam);
					
			//레이아웃에 넣기
			layout.addView(seq);
			layout.addView(edit);
			layout.addView(text);
			
			main_layout.addView(layout);
		}
	}

	@Override
	public void threadCompleted(boolean isNetWordConnected, Object result,
			String message) {
		if(null != myapp.dialog || myapp.dialog.isShowing()){
			myapp.dialog.dismiss();
		}
		
		if((Boolean) result){
			msg = handler.obtainMessage();
			msg.what = MSG_THREAD_COMPLETED;
			handler.sendMessage(msg);
		}
	}

	@Override
	public void threadError(String message) {
		
	}
	
}
